package com.cmbchina.encrypt;

/**
 * @author liuxb
 */
public abstract class AbstractPadding {
    protected final int blockSize;
    protected final boolean optional;

    public AbstractPadding(int blockSize) {
        if (blockSize > 0 && blockSize < 256) {
            this.optional = false;
            this.blockSize = blockSize;
        } else {
            throw new IllegalArgumentException(String.format("Illegal block size %d.", blockSize));
        }
    }

    public AbstractPadding(int blockSize, boolean optional) {
        if (blockSize > 0 && blockSize < 256) {
            this.blockSize = blockSize;
            this.optional = optional;
        } else {
            throw new IllegalArgumentException(String.format("Illegal block size %d.", blockSize));
        }
    }

    public abstract byte[] pad(byte[] var1, int var2, int var3);

    public abstract byte[] unpad(byte[] var1, int var2, int var3);

    public abstract int outputSize(int var1);

    public byte[] pad(byte[] input) {
        if (input == null) {
            throw new IllegalArgumentException("The data to be padding cannot be NULL.");
        } else {
            return this.pad(input, 0, input.length);
        }
    }

    public byte[] unpad(byte[] input) {
        if (input == null) {
            throw new IllegalArgumentException("The data to be padding cannot be NULL.");
        } else {
            return this.unpad(input, 0, input.length);
        }
    }

    public int pad(byte[] input, byte[] output, int outputOffset) {
        byte[] data = this.pad(input, 0, input.length);
        System.arraycopy(data, 0, output, outputOffset, data.length);
        return data.length;
    }

    public int unpad(byte[] input, byte[] output, int outputOffset) {
        byte[] data = this.unpad(input, 0, input.length);
        System.arraycopy(data, 0, output, outputOffset, data.length);
        return data.length;
    }

    public int pad(byte[] input, int offset, int len, byte[] output, int outputOffset) {
        byte[] data = this.pad(input, offset, len);
        System.arraycopy(data, 0, output, outputOffset, data.length);
        return data.length;
    }

    public int unpad(byte[] input, int offset, int len, byte[] output, int outputOffset) {
        byte[] data = this.unpad(input, offset, len);
        System.arraycopy(data, 0, output, outputOffset, data.length);
        return data.length;
    }
}